import { createRouter, createWebHistory } from 'vue-router'
import CryptoJS from 'crypto-js'

import { storeToRefs } from 'pinia'
import { useStore  } from '../store/index'


import CSGL from '../views/CSGL/CSGL.vue'
import DJJS from '../views/DJJS/DJJS.vue'
import JBQK from '../views/JBQK/JBQK.vue'
import JJYX from '../views/JJYX/JJYX.vue'
import ZWFW from '../views/ZWFW/ZWFW.vue'
import Error from '../views/Error/Error.vue'
const routes = [
  {
    path: '',
    redirect: "/JBQK",
  },
  {
    path: '/CSGL',
    name: '城市管理',
    component: CSGL
  },
  {
    path: '/DJJS',
    name: '党建建设',
    component: DJJS
  },
  {
    path: '/JBQK',
    name: '基本情况',
    component: JBQK
  },
  {
    path: '/JJYX',
    name: '经济运行',
    component: JJYX
  },
  {
    path: '/ZWFW',
    name: '政务服务',
    component: ZWFW
  },
  {
    path: '/Error',
    name: '授权错误',
    component: Error
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

//判断项目是否授权
router.beforeEach((to, from, next) => {
  let jieMiStr = ''
  let keyValue = ''
  const paw = CryptoJS.enc.Utf8.parse("XA_ZCSC_PA123456");
  // 十六位十六进制数作为密钥偏移量
  const iv = CryptoJS.enc.Utf8.parse("XA_ZCSC_IV123456");
  if (window.keyConfig) {
    keyValue = window.keyConfig.key;
    const key = CryptoJS.enc.Utf8.parse(paw);
    const decrypt = CryptoJS.AES.decrypt(keyValue, key, { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 });
    jieMiStr = (CryptoJS.enc.Utf8.stringify(decrypt).toString()).split("+");
    /**
  * 获取当前时间
  */
    let currentDate = new Date();
    let currentYear = currentDate.getFullYear();
    let currentMonth = currentDate.getMonth() + 1;
    let currentDay = currentDate.getDate();
    let placeholderStart = Number(`${currentYear}${currentMonth}${currentDay}`)
    /**
     * 开始解密
     */

    //判断解密后文件是否正确
    if (jieMiStr.length == 3) {
      let timeStart = Number(jieMiStr[1].replace(new RegExp('-', "g"), ""));
      let timeEnd = Number(jieMiStr[2].replace(new RegExp('-', "g"), ""));
      //判断项目是否对应
      if (jieMiStr[0].replace(/\s/g, "") == 'vue3DemoCooCity') {
        //判断开始时间
        if (timeStart == placeholderStart || timeStart < placeholderStart) {
          //判断结束时间
          if (timeEnd == placeholderStart || timeEnd > placeholderStart) {
            const store = useStore()
            let { commonStyle }  = storeToRefs(store);
            console.log(commonStyle.value);
            //执行进入项目
            store.$patch(state=>{
              state.commonStyle = true;
             
            })
           
            next()
          } else {

            if (to.path == '/Error') {
              next()
            } else {
              next({
                path: '/Error',
                query: '5'
              })
            }
          }
        } else {
          if (to.path == '/Error') {
            next()
          } else {
            next({
              path: '/Error',
              query: '4'
            })
          }
        }
      } else {

        if (to.path == '/Error') {
          next()
        } else {
          next({
            path: '/Error',
            query: '3'
          })
        }
      }
    } else {
      if (to.path == '/Error') {
        next()
      } else {
        next({
          path: '/Error',
          query: '2'
        })
      }
    }
  } else {
    if (to.path == '/Error') {
      next()
    } else {
      next({
        path: '/Error',
        query: '1'
      })
    }

  }
})

export default router
